/**
 * Copyright (C) 2012 LightAsyncHttpClient Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.topicbank.http;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

import com.lurencun.android.topicbank.http.LightAsyncHttp.InvokeCallback;

/**
 * <ul>
 * <li><b>name : </b>		HttpRequestInvoker		</li>
 * <li><b>description :</b>	执行Http请求				</li>
 * <li><b>author : </b>		桥下一粒砂			</li>
 * <li><b>e-mail : </b>		chenyoca@gmail.com	</li>
 * <li><b>weibo : </b>		@桥下一粒砂			</li>
 * <li><b>date : </b>		2012-9-22 上午1:26:50		</li>
 * </ul>
 */
public class RequestInvoker implements Runnable {

	private final DefaultHttpClient mHttpClient;
	
	private final HttpContext mHttpContext;
	
	private final HttpUriRequest mUriRequest;
	
	private final ResponseCallback mRespCallback;
	
	private final InvokeCallback mExecutionCallback;
	
	private final int mRequestId;
	
	public RequestInvoker(DefaultHttpClient httpClient, HttpContext httpContext, HttpUriRequest uriRequest,
			ResponseCallback responseCallback,InvokeCallback executionCallback,int requestId){
		
		mHttpClient = httpClient;
		mHttpContext = httpContext;
		mUriRequest = uriRequest;
		mRespCallback = responseCallback;
		mExecutionCallback = executionCallback;
		mRequestId = requestId;
	}
	
	@Override
	public void run() {
		
		final String url = mUriRequest.getURI().toString();
		mRespCallback.onStart(url);
		
		//可处理的异常则自动重连
		boolean retryable = true;
		int executionCount = 0;
		HttpRequestRetryHandler retryHandler = mHttpClient.getHttpRequestRetryHandler();
		while(retryable){
			
			try {
				if(!Thread.currentThread().isInterrupted()) {
					HttpResponse response = mHttpClient.execute(mUriRequest);
					if(!Thread.currentThread().isInterrupted()){
						mRespCallback.onSuccess(mRequestId, response,url);
					}else{
						mRespCallback.onCancel(mRequestId,url);
					}
				}else{
					mRespCallback.onCancel(mRequestId,url);
				}
				break;
			} catch ( IOException exp ){
				//IO异常，重试处理
				exp.printStackTrace();
				retryable = retryHandler.retryRequest(exp, ++executionCount, mHttpContext);
				if(!retryable) mRespCallback.onError(new Throwable(exp.getMessage()), mRequestId, url);
				// retryable will break while loop
			} catch ( Exception exp ){
				//不可处理的异常
				exp.printStackTrace();
				mRespCallback.onError(new Throwable(exp.getMessage()), mRequestId, url);
				break;
			}
		}
		mExecutionCallback.sendMessage(mRequestId);
	}
}
